package com.u.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.u.Repiar.RepairItem_info;
import com.u.character.Manager;
import com.u.character.Processer;
import com.u.character.Repairer;
import com.u.character.Role;
import com.u.character.Station;
import com.u.eventInfo.Event;
import com.u.eventInfo.EventInfo;
import com.u.tool.MysqlUtil;

/**
 * Servlet implementation class ModifyEvent
 */
@WebServlet("/ModifyEvent")
public class ModifyEvent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyEvent() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
						
		String event_id = request.getParameter("event_id");
		String event_item = request.getParameter("event_item");
		String context = request.getParameter("context");
		String applier_name = request.getParameter("applier_name");
		String applier_tel = request.getParameter("applier_tel");
		MysqlUtil db = new MysqlUtil();
		db.connection();
		String sql = "SELECT * FROM y_r.event where event_id='" + event_id + "'";
		List list = new ArrayList<>();
		System.out.println(list.size());
		list = db.query(Event.class, sql);
		Event event = (Event) list.get(0);
		
		HttpSession session = request.getSession();
		Role role = (Role) session.getAttribute("Role");
		boolean useracc = false;
		switch(role.getUsertype()) {
		case("manager"):useracc = false;break;
		case("processer"):{
			Processer processer = new Processer(role);
			if((processer.getServiceType()).equals(event.getEvent_type())) {
				useracc = true;
			}else {
				useracc = false;
			}
			break;
		}
		case("station"):{
			Station station = new Station(role);
			if((station.getStationCode()).equals(event.getStation_id())) {
				useracc = true;
			}else {
				useracc = false;
			}
			break;
		}
		case("repairer"):useracc = false;break;
		default:break;
		}
		if(useracc) {
			sql = "SELECT * FROM y_r.eventinfo where event_id='" + event_id + "'";
			db.connection();
			list = db.query(EventInfo.class, sql);
			EventInfo eventInfo = (EventInfo) list.get(0);
			sql = "SELECT * FROM y_r.repair_item where item_id='"+ event_item +"'";
			db.connection();
			list = db.query(RepairItem_info.class, sql);
			RepairItem_info info = (RepairItem_info) list.get(0);
			
			String event_type = info.getType();
			String event_item_name = info.getItem_name();
			System.out.println(event_type);
			
			event.setEvent_item(event_item_name);
			event.setItem_id(event_item);
			event.setEvent_type(event_type);
			
			eventInfo.setEvent_item(event_item_name);
			eventInfo.setEvent_description(context);
			eventInfo.setApplier_name(applier_name);
			eventInfo.setApplier_tel(applier_tel);
			
			db.connection();
			db.update(event);
			db.connection();
			db.update(eventInfo);
		}
		
		response.sendRedirect("/r/eventdetail.jsp?event_id="+event_id+"&useracc="+useracc);
	}

}
